import {
    Coupon
} from './coupon-model.js';
var coupon = new Coupon();

Page({
    /**
     * 页面的初始数据
     **/
    data: {
        now: 0,
        myCouponList: {},
    },
    /*
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showNavigationBarLoading()
        this.commonRequest('1')
    },
    swiperChange(e) {
        console.log(e.detail.current)
        this.setData({
            now: e.detail.current
        })
        wx.showNavigationBarLoading()
        this.commonRequest(Number(e.detail.current + 1))
    },
    toggleSwiper(e) {
        this.setData({
            now: e.currentTarget.dataset.id
        })
    },
    commonRequest(type, cb) {
        let params = {
            filter: '' + type
        }
        coupon.myCouponList(params, res => {
            wx.hideNavigationBarLoading()
            wx.stopPullDownRefresh()
            if (res.status) {
                let myCouponList = "myCouponList." + type
                res.data.forEach(val => {
                    val.startTime = coupon.formate(val.startTime, 'yyyy-MM-dd')
                    val.endTime = coupon.formate(val.endTime, 'yyyy-MM-dd')
                    if (val.pcuEndTime && val.pcuStartTime) {
                        val.pcuEndTime = coupon.formate(val.pcuEndTime, 'yyyy-MM-dd')
                        val.pcuStartTime = coupon.formate(val.pcuStartTime, 'yyyy-MM-dd')
                    }
                })
                this.setData({
                    [myCouponList]: res.data
                })
            } else {
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })
    },
    onPullDownRefresh() {
        this.commonRequest('1')
    },
    jumpActivity(e){
        let id = coupon.getDataSet(e,'id');
        if(id){
            wx.navigateTo({
                url: '/pages/home/home?id='+id,
            })
        }else{
            wx.showToast({
                title: '优惠券关联活动未开始',
                icon:'none'
            })
        }
    }

})